﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PressMenu2
{
    class GameObject
    {
        protected int player;
        protected int id;
        protected Texture2D texture;
        protected Vector2 position;  
        protected Rectangle rectangle;
        protected float angle=0;
        private int blood = 10;   // máu
        protected int power = 1;    // độ mạnh của quân, là số máu mà đối phương bị trừ khi va phải.

        public virtual void Render(SpriteBatch sb)
        {
            //sb.Draw(texture, rectangle, Color.White);
            Rectangle srcRect = new Rectangle(0, 0, texture.Width, texture.Height);
            sb.Draw(texture, position, srcRect, Color.White, angle, new Vector2(texture.Width / 2, texture.Height), new Vector2(1,1),SpriteEffects.None,1f);
            sb.DrawString(MyResource.fontArial15,blood.ToString(),position + new Vector2(5,-60),Color.Blue);
        }

        /// <summary>
        /// return
        ///      0: ko co gi
        ///      1: ban mui ten
        ///      2: đĩa bay thoát, trừ blood của người chơi.
        ///     -1: huy doi tuong.
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public virtual int Update(GameTime gameTime, MyInput input)
        {
            return 0;
        }

        public virtual bool CheckCollision(GameObject gameObject)
        {
            return CheckCollision(gameObject.rectangle);
        }

        public virtual bool CheckCollision(Rectangle rect)
        {
            Vector2[] a = new Vector2[4];
            a[0] = new Vector2(rect.Left, rect.Top);
            a[1] = new Vector2(rect.Right, rect.Top);
            a[2] = new Vector2(rect.Right, rect.Bottom);
            a[3] = new Vector2(rect.Left, rect.Bottom);
            for (int i = 0; i < 4; i++ )
                if(CheckCollision(a[i]))
                    return true;
            return false;
        }

        public virtual bool CheckCollision(Vector2 v)
        {
            if (v.X > rectangle.Left && v.X < rectangle.Right)
                if (v.Y > rectangle.Top && v.Y < rectangle.Bottom)
                    return true;
            return false;
        }

        public virtual void Collision(int power)
        {
            blood -= power;
            if (blood <= 0)
                Die();
        }

        private void Die()
        {
        }

        public GameObject(int id, int player)
        {
            this.id = id;
            this.player = player;
            this.position = new Vector2();
            this.rectangle = new Rectangle();
        }

        public GameObject()
        {
        }


        protected Vector2 Position
        {
            get { return position; }
            set {
                position = value;
                rectangle.X = (int)position.X;
                rectangle.Y = (int)position.Y;
            }
        }


        public virtual float Angle
        {
            get { return angle; }
            set { angle = value; }
        }

        public int Power
        {
            get { return power; }
            set { power = value; }
        }


        public int Blood
        {
            get { return blood; }
            set { blood = value; }
        }
    }
}
